package com.sleepbot.datetimepicker.time;


import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import com.fourmob.datetimepicker.ResourceTable;
/**
 * Draws a simple white circle on which the numbers will be drawn.
 */
public class CircleView extends Component implements Component.DrawTask {
    private static final String TAG = "CircleView";
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "DateTimePicker");
    private final Paint mPaint = new Paint();
    private boolean mIs24HourMode;
    private int mWhite;
    private int mBlack;
    private float mCircleRadiusMultiplier;
    private float mAmPmCircleRadiusMultiplier;
    private boolean mIsInitialized;

    private boolean mDrawValuesReady;
    private int mXCenter;
    private int mYCenter;
    private int mCircleRadius;

    public CircleView(Context context) {
        super(context);
        ResourceManager resourceManager = context.getResourceManager();
        try {
           mWhite = resourceManager.getElement(ResourceTable.Color_white).getColor();
           mBlack = resourceManager.getElement(ResourceTable.Color_numbers_text_color).getColor();
        }catch (Exception e) {
            e.printStackTrace();
        }
        mPaint.setAntiAlias(true);
        mIsInitialized = false;
        addDrawTask(this::onDraw);
    }

    public void initialize(Context context, boolean is24HourMode) {
        if (mIsInitialized) {
            HiLog.error(label, "CircleView may only be initialized once.");
            return;
        }

        ResourceManager resourceManager = context.getResourceManager();
        mIs24HourMode = is24HourMode;
        if (is24HourMode) {
            try {
                mCircleRadiusMultiplier = Float.parseFloat(
                        resourceManager.getElement(ResourceTable.String_circle_radius_multiplier_24HourMode).getString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                mCircleRadiusMultiplier = Float.parseFloat(
                       resourceManager.getElement(ResourceTable.String_circle_radius_multiplier).getString());
                mAmPmCircleRadiusMultiplier = Float.parseFloat(
                        resourceManager.getElement(ResourceTable.String_ampm_circle_radius_multiplier).getString());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        mIsInitialized = true;
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int viewWidth = getWidth();
        if (viewWidth == 0 || !mIsInitialized) {
            return;
        }
        if (!mDrawValuesReady) {
            mXCenter = getWidth() / 2;
            mYCenter = getHeight() / 2;
            mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier);

            if (!mIs24HourMode) {
                // We'll need to draw the AM/PM circles, so the main circle will need to have
                // a slightly higher center. To keep the entire view centered vertically, we'll
                // have to push it up by half the radius of the AM/PM circles.
                int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier);
                mYCenter -= amPmCircleRadius / 2;
            }

            mDrawValuesReady = true;
        }
        mPaint.setColor(new Color(mWhite));
        canvas.drawCircle(mXCenter, mYCenter, mCircleRadius, mPaint);

        // Draw a small black circle in the center.
        mPaint.setColor(new Color(mBlack));
        canvas.drawCircle(mXCenter, mYCenter, 2, mPaint);

    }

}